// Maps uuid -> metadata object
const extensions = {};
+/**
+ * getCurrentExtension:
+ *
+ * Returns the current extension, or null if not called from an extension.
+ */
function getCurrentExtension() {
- let stack = (new Error()).stack;
-
- // Assuming we're importing this directly from an extension (and we shouldn't
- // ever not be), its UUID should be directly in the path here.
- let extensionStackLine = stack.split('\n')[1];
+ let stack = (new Error()).stack.split('\n');
+ let extensionStackLine;
+
+ // Search for an occurrence of an extension stack frame
+ // Start at 1 because 0 is the stack frame of this function
+ for (let i = 1; i < stack.length; i++) {
+ if (stack[i].indexOf('/gnome-shell/extensions/') > -1) {
+ extensionStackLine = stack[i];
+ break;
+ }
+ }
if (!extensionStackLine)
- throw new Error('Could not find current extension');
+ return null;
// The stack line is like:
// init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
// @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
if (!match)
- throw new Error('Could not find current extension');
+ return null;
let path = match[1];
let file = Gio.File.new_for_path(path);
file = file.get_parent();
}
- throw new Error('Could not find current extension');
+ return null;
}
/**